<?php

class m_barangsupply extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_barangsupply() {
        $sql = "SELECT COUNT(barangsupply_cd)'total' FROM barangsupply";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    function get_count_all_barangsupply_search($params) {
        $sql = "SELECT COUNT(*)'total' FROM barangsupply a              
                WHERE a.barangsupply_cd LIKE ? 
                AND a.barangsupply_name LIKE ? ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_barangsupply_by_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM barangsupply WHERE barangsupply_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_barangsupply_by_milik_cd_barangsupply_name($params) {
        $sql = "SELECT COUNT(*) 'total' FROM barangsupply WHERE milik_cd = ? AND barangsupply_name = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_barangsupply_by_kategoribarang_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM barangsupply WHERE kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_barangsupply() {
        $sql = "SELECT a.*FROM barangsupply a               
                ORDER BY a.barangsupply_name ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_barangsupply_pagination($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM barangsupply a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                ORDER BY b.kategoribarang_name, a.barangsupply_cd ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_barangsupply_pagination_search($params) {
        $sql = "SELECT a.* FROM barangsupply a
                WHERE a.barangsupply_cd LIKE ? 
                AND a.barangsupply_name LIKE ? 
                ORDER BY a.barangsupply_name ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_barangsupply_by_barangsupply_cd($params) {
        $sql = "SELECT a.* FROM barangsupply a

                WHERE a.barangsupply_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO barangsupply VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE barangsupply SET barangsupply_name = ?,  barangsupply_keterangan = ?, mdb = ? WHERE barangsupply_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM barangsupply WHERE barangsupply_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    function get_jenis_by_kategoribarang_cd_laporan($param) {
        $sql = "SELECT a.*, b.*, c.* FROM barangsupply a
                JOIN kategoribarang b ON a.kategoribarang_cd = b.kategoribarang_cd
                JOIN group_barang c ON b.gpbarang_cd = c.gpbarang_cd
                WHERE a.kategoribarang_cd = ?";
        $query = $this->db->query($sql, $param);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}
